package com.ruoyi.web.controller.erp;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.erp.common.enums.OrderProductStatus;
import com.ruoyi.erp.common.enums.OrderStatus;
import com.ruoyi.erp.common.enums.ProductType;
import com.ruoyi.erp.domain.XzgProduct;
import com.ruoyi.erp.domain.dto.XzgOrderExcelDto;
import com.ruoyi.erp.domain.dto.XzgOrderProductDto;
import com.ruoyi.erp.domain.vo.XzgOrderProductVo;
import com.ruoyi.erp.service.IXzgProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author qiuwencong
 * @Date 2021/12/31 10:01
 * @Package com.ruoyi.web.controller.erp
 * @Description
 */
@RestController
@RequestMapping("/erp/product")
public class ProductController extends BaseController {

    @Autowired
    private IXzgProductService productService;

    /**
     * 添加产品信息
     *
     * @param product
     * @return
     */
    @PostMapping
    public AjaxResult add(@Validated @RequestBody XzgProduct product) {
        product.setCreateBy(getUsername());
        product.setUpdateBy(getUsername());
        product.setCreateTime(new Date());
        product.setUpdateTime(new Date());
        return toAjax(productService.addProduct(product));
    }

    /**
     * 检查是否有相同的产品名字
     *
     * @param productId
     * @param productName
     * @return
     */
    @GetMapping("/checkName")
    public AjaxResult checkSameName(Long productId, String productName) {
        return AjaxResult.success(productService.checkProductName(productId, productName));
    }

    /**
     * 检查是否有相同的产品编号
     *
     * @param productId
     * @param productCode
     * @return
     */
    @GetMapping("/checkCode")
    public AjaxResult checkSameCode(Long productId, String productCode) {
        return AjaxResult.success(productService.checkProductCode(productId, productCode));
    }

    /**
     * 获取分页产品列表
     *
     * @param product
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo list(XzgProduct product) {
        startPage();
        List<XzgProduct> productList = productService.selectProductList(product);
        return getDataTable(productList);
    }

    /**
     * 根据商品id获取商品信息
     *
     * @param productId
     * @return
     */
    @GetMapping("/{productId}")
    public AjaxResult getById(@PathVariable Long productId) {
        return AjaxResult.success(productService.selectProductById(productId));
    }

    /**
     * 根据id修改商品信息
     *
     * @return
     */
    @PutMapping
    public AjaxResult update(@Validated @RequestBody XzgProduct product) {
        product.setUpdateBy(getUsername());
        product.setUpdateTime(new Date());
        return toAjax(productService.updateProduct(product));
    }

    /**
     * 逻辑删除数据
     *
     * @param productIds
     * @return
     */
    @DeleteMapping("/{productIds}")
    public AjaxResult delete(@PathVariable Long[] productIds) {
        return toAjax(productService.deleteProductById(productIds, getUsername()));
    }

    /**
     * 根据产品编号和产品名称获取产品信息
     *
     * @param name
     * @return
     */
    @GetMapping
    public AjaxResult get(String name) {
        return AjaxResult.success(productService.selectByCodeOrName(name));
    }

    /**
     * 查询指定商品的库存
     *
     * @param productId
     * @return
     */
    @GetMapping("/count/{productId}")
    public AjaxResult selectCount(@PathVariable Long productId) {
        return AjaxResult.success(productService.selectCountById(productId));
    }

    /**
     * 下载模板
     *
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<XzgProduct> util = new ExcelUtil<>(XzgProduct.class);
        String[] strings = Arrays.stream(ProductType.values()).map(ProductType::getValue).toArray(String[]::new);
        util.setDropDownSelection(strings);
        util.setSelectRange(new int[]{1, 200, 2, 2});
        util.importTemplateExcel(response, "产品数据");
    }

    /**
     * 导入
     *
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        ExcelUtil<XzgProduct> util = new ExcelUtil<>(XzgProduct.class);
        List<XzgProduct> products = util.importExcel(file.getInputStream());
        return AjaxResult.success(productService.importProducts(products, updateSupport, getUsername()));
    }

    /**
     * 导出
     *
     * @param response
     * @param product
     */
    @PostMapping("/export")
    public void export(HttpServletResponse response, XzgProduct product) {
        List<XzgProduct> productList = productService.selectProductList(product);
        for (XzgProduct xzgProduct : productList) {
            xzgProduct.setProductType(ProductType.getValueByKey(xzgProduct.getProductType()));
        }
        ExcelUtil<XzgProduct> util = new ExcelUtil<>(XzgProduct.class);
        util.exportExcel(response, productList, "产品数据");
    }
}
